/**
 */
package com.yunda.AJDeviceService.controller;

import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.yunda.AJDeviceService.common.web.domain.AjaxResult;
import com.yunda.AJDeviceService.data.DeviceRunStatusData;
import com.yunda.AJDeviceService.data.DeviceRunTimeData;
import com.yunda.AJDeviceService.data.IntelligentCheckDetailData;
import com.yunda.AJDeviceService.service.DeviceRunStatusService;
import com.yunda.AJDeviceService.service.DeviceRunTimeService;
import com.yunda.AJDeviceService.service.DetailDataService;
import com.yunda.AJDeviceService.utils.PictureUtils;

import lombok.extern.slf4j.Slf4j;

/**
 * AJDeviceService
 * @description 安检机上传数据处理
 * @author ljc
 * @create 2023年2月10日 下午3:30:45
 */
@RestController
@RequestMapping(value="/uploadData")
@Slf4j
public class UploadDataController {
	
	String osName = System.getProperty("os.name").toLowerCase();
	@Autowired
	private DetailDataService detailDataService;
	@Autowired
	private DeviceRunTimeService deviceRunTimeService;
	@Autowired
	private DeviceRunStatusService deviceRunStatusService;
	
	/**
	 * TODO 安检明细数据上传
	 * @param detailData
	 * @return 
	 * AjaxResult
	 */
	@RequestMapping(value="/uploadDetailData",method = RequestMethod.POST)
	public AjaxResult uploadDetailData(@RequestBody IntelligentCheckDetailData detailData) {
    	log.info(" *** 上传安检明细数据：{}",JSON.toJSON(detailData));
        if(!ObjectUtils.isEmpty(detailData)) {
            boolean result=detailDataService.saveDetailData(detailData);
            if(result) {
                return AjaxResult.success("安检机明细数据上传成功");
            }else {
            	log.error(" *** 安检明细数据上传失败：{}",JSON.toJSON(detailData));
            }
        }
        return AjaxResult.error("上传安检机明细数据为空");
    }
	
	
	@RequestMapping(value="/uploadDeviceRunTime",method = RequestMethod.POST)
	public AjaxResult uploadDeviceRunTime(@RequestBody DeviceRunTimeData runTimeData) {
    	log.info(" *** 上传安检运行时间：{}",JSON.toJSON(runTimeData));
        if(!ObjectUtils.isEmpty(runTimeData)) {
            boolean result=deviceRunTimeService.save(runTimeData);
            if(result) {
                return AjaxResult.success("安检机运行时间上传成功");
            }else {
            	log.error(" *** 安检机运行时间上传失败：{}",JSON.toJSON(runTimeData));
            }
        }
        return AjaxResult.error("上传安检机运行时间为空");
    }

	/**
	 * TODO 安检机运行状态上传
	 * @param statusData
	 * @return 
	 * AjaxResult
	 */
	@RequestMapping(value="/uploadDeviceRunStatus",method = RequestMethod.POST)
	public AjaxResult uploadDeviceRunStatus(@RequestBody DeviceRunStatusData statusData) {
    	log.info(" *** 上传安检运行状态：{}",JSON.toJSON(statusData));
        if(!ObjectUtils.isEmpty(statusData)) {
            boolean result=deviceRunStatusService.uploadDeviceStatus(statusData);
            if(result) {
                return AjaxResult.success("安检机运行状态上传成功");
            }else {
            	log.error(" *** 安检机运行状态上传失败：{}",JSON.toJSON(statusData));
            }
        }
        return AjaxResult.error("上传安检机运行状态数据为空");
    }
	
	/**
	 * TODO  违禁品图片上传
	 * @param imageFile
	 * @param scanDate
	 * @param fbCode
	 * @param deviceCode
	 * @return 
	 * AjaxResult
	 */
    @RequestMapping(value="/uploadImage",method = RequestMethod.POST)
    public AjaxResult getProcessData(@RequestParam("imageFile") MultipartFile imageFile, @RequestParam("scanDate") String scanDate
    		, @RequestParam("fbCode") String fbCode, @RequestParam("deviceCode") String deviceCode) {
    	FileInputStream in = null;
    	try {
			String imageName=imageFile.getOriginalFilename();
			String mkdirStr="";
	        if(osName.contains("windows")){
	        	mkdirStr="F:\\u02\\data\\inteCheckImg\\"+scanDate.replaceAll("-", "")+"\\"+fbCode+"\\"+deviceCode+"\\";
	        }else {
	        	mkdirStr="//u02/data/inteCheckImg/"+ scanDate +"/"+ fbCode +"/"+ deviceCode+"/";
	        }
	        in = (FileInputStream) imageFile.getInputStream();
	        BufferedImage image = ImageIO.read(in);
	        PictureUtils.createImage(image, mkdirStr, imageName);
			return AjaxResult.success("违禁品图片上传成功");
		} catch (Exception e) {
	        return AjaxResult.error("违禁品图片上传失败"+e.getMessage());
		}finally {
            if(in != null) {
                try {
                	in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
   
}
